import logging
from django.db import connections

class ZhongchengDBQueryTool:
    def __init__(self, database='zhongcheng_db'):
        self.database = database

    def execute_query(self, query, params=None):
        """
        Execute a raw query on 'zhongcheng_db' database.
        
        :param query: str, SQL query to be executed.
        :param params: tuple or list, parameters to be included in the query.
        :return: list, fetched rows.
        """
        try:
            with connections[self.database].cursor() as cursor:
                cursor.execute(query, params or [])
                rows = cursor.fetchall()
                return rows
        except Exception as e:
            logging.error(f"An error occurred while executing the query: {str(e)}")
            return []
    def execute_update(self, query, params=None):
        """
        Execute an UPDATE (or INSERT/DELETE) query on the database.
        
        :param query: str, SQL query to be executed.
        :param params: tuple or list, parameters to be included in the query.
        :return: int, the number of affected rows or -1 if an error occurs.
        """
        try:
            with connections[self.database].cursor() as cursor:
                cursor.execute(query, params or [])
                return cursor.rowcount  # Return the number of affected rows
        except Exception as e:
            logging.error(f"An error occurred while executing the update: {str(e)}")
            return -1
